package xerr

import "fmt"

type BioError interface {
	GetErrCode() int64
	GetErrMsg() string
	Error() string
}

// 常用通用固定错误
type CodeError struct {
	errCode int64
	errMsg  string
}

type CodeErrorResponse struct {
	Code int    `json:"code"`
	Msg  string `json:"msg"`
}

// 返回给前端的错误码
func (e *CodeError) GetErrCode() int64 {
	return e.errCode
}

// 返给前端显示错误信息
func (e *CodeError) GetErrMsg() string {
	return e.errMsg
}

func (e *CodeError) Data() *CodeErrorResponse {
	return &CodeErrorResponse{
		Code: int(e.errCode),
		Msg:  e.errMsg,
	}
}

func (e *CodeError) Error() string {
	return fmt.Sprintf("ErrCode:%d, ErrMsg:%s", e.errCode, e.errMsg)
}

func NewErrCodeMsg(errCode int64, errMsg string) *CodeError {
	return &CodeError{errCode: errCode, errMsg: errMsg}
}

func NewErrCode(errCode int64) *CodeError {
	return &CodeError{errCode: errCode, errMsg: MapErrMsg(errCode)}
}

func NewErrMsg(errMsg string) *CodeError {
	return &CodeError{errCode: SERVER_COMMON_ERROR, errMsg: errMsg}
}
